package com.mxx.tomcat;


import com.mxx.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class WebExceptionResolver implements HandlerExceptionResolver {

    private ResponseWriter responseWriter;

    public WebExceptionResolver(ResponseWriter responseWriter) {
        this.responseWriter = responseWriter;
    }

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        try {
            log.error("uri: " + request.getRequestURI() + ", message :" + e.getMessage() + "    ", e);
            responseWriter.writer(response, new Response<>(500, e.getMessage()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return new ModelAndView();
    }
}
